home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
5 Star Games: DOS Edition 2
/
5 Star Games - DOS Edition (1995)(Ready to Run).iso
/
dbc
/
db_work.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-07
|
14KB
|
556 lines
/****************************************************************************/
/* DATABOSS MODULE: DB_WORK.C */
/* Set the editor TAB size to 3 for correct indentation */
/****************************************************************************/
#include "db_lsc.h"
#include <conio.h>
#ifndef __TURBOC__
#include <graph.h>
#include <sys\types.h>
#endif
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <errno.h>
#include "db_types.h"
#include "db_conio.h"
#include "db_crit.h"
#include "db_curs.h"
#include "db_file.h"
#include "db_funcs.h"
#include "db_gvar.h"
#include "db_heap.h"
#include "db_key.h"
#include "db_str.h"
#include "db_win.h"
#include "db_mnu.h"
#include "db_util.h"
#include "db_uwin.h"
#include "db_work.h"
/**************************** GLOBAL VARIABLES ****************************/
helpproc choosehelp;
bool permitesc;
/*************************** INTERNAL VARIABLES ***************************/
static string choices_;
static byte dispwid_,wwid_,whgt_,numc_,cnum_;
static winptr twinp_;
static uchar cmd_;
static pathstr chkmask;
static bool initialized = False;
/***************************** IMPLEMENTATION *****************************/
void defChooseHelp(void) /* SN 3.5 */
{
winptr tempWindow; /* SN 3.5 */
tempWindow = ChooseHelpWin(); /* SN 3.5 */
disphelpwin(tempWindow); /* SN 3.5 */
closewin(&tempWindow); /* SN 3.5 */
return; /* SN 3.5 */
}
bool chkpickname(string instr)
{
string ts;
if (*chkmask == '\0') strcpy(chkmask,"*.*");
if (exitcode == F2) {
dirpick(ts,chkmask,DirIncl,DirExcl,0,0);
strcpy(instr,ts);
exitcode = ' ';
}
return(True);
}
strptr removeparm(strptr sout, strptr wkstr, int p1)
{
int x, c, sourceLength;
sourceLength = strlen(wkstr);
if (*(wkstr + (p1+2)) == '(') {
c = 1;
x = p1 + 3;
while ((x < sourceLength) && (c > 0)) {
if (*(wkstr + x) == ')') c--;
else if (*(wkstr + x) == '(') c++;
if (c > 0) x++;
}
strcopy(sout,wkstr,p1+3,x-(p1+3));
strdelete(wkstr,p1+2,x-(p1+1));
}
else
*sout = '\0';
return(sout);
}
bool replmacro(strptr wkstr)
{
/*
Macro of the form :--
¿<ID Letter>[(<Parameter>) ...]
<ID Letter> Parameter
------------------------------------------------ -------------------------
@ -- Prompt for user input string User Prompt
C -- Choose box CData and CList
D -- Directory listing File Mask
E -- returns environment variable Environment Variable Name
K -- returns -K if enhanced keyboard is off NONE
L -- returns -LCD if Liquid Display switch is on NONE
*/
uchar mode;
bool fval;
int p1,
p2;
uchar ts[81],
ws1[81],
ws2[81];
uchar tpic[4];
strcpy(tpic, "80X");
tpic[2] = Pic_X;
fval = True;
p1 = strposch('¿',wkstr);
while (fval && (p1 >= 0)) {
mode = wkstr[p1+1];
ts[0] = '\0';
switch (mode) {
case 'L' : if (lcd) strcpy(ts,"-LCD"); break;
case 'K' : if (!ehk) strcpy(ts,"-K"); break;
case '@' :
removeparm(ws1,wkstr,p1);
fval = replmacro(ws1);
if (fval) {
trim(ts,dbgetstr(ts,_Ch,ws1,"",tpic,"",nocheck,nohelp));
fval = exitcode != QitKey;
}
break;
case 'C' :
removeparm(ws1,wkstr,p1);
fval = replmacro(ws1);
if (fval) {
removeparm(ws2,wkstr,p1);
if (ws2[0] == '\0') strcpy(ws2,ws1);
fval = replmacro(ws2);
if (fval) {
fval = permitesc;
permitesc = True;
choose(ws1,ws1,ws2,ts);
permitesc = fval;
fval = exitcode != QitKey;
}
}
break;
case 'D' :
removeparm(ws1,wkstr,p1);
fval = replmacro(ws1);
if (fval) {
removeparm(chkmask,wkstr,p1);
fval = replmacro(chkmask);
if (fval) {
if (chkmask[0] == '\0') {
strcpy(chkmask,ws1);
ws1[0] = '\0';
}
if (ws1[0] != '\0')
trim(ts,dbgetstr(ts,_Ch,ws1,"",tpic,"",chkpickname,nohelp));
else
dirpick(ts,chkmask,DirIncl,DirExcl,0,0);
fval = (ts[0] != '\0');
}
}
break;
case 'E' :
removeparm(ws1,wkstr,p1);
fval = replmacro(ws1);
if (fval) strcpy(ts,getenv(ws1));
break;
} /* switch (mode) */
strinsert(ts,wkstr,p1+2); strdelete(wkstr,p1,2);
p1 = strposch('¿',wkstr);
}
return(fval);
}
/* SN 3.5 - the padout function is completely new for 3.5 */
uchar * padout(uchar *destination, uchar *theSource)
{
int arrayIndex, arraySize, longestElementSize;
uchar array[25][80];
STRING localSource;
STRPTR source;
uchar *sourceStart, *sourceEnd;
uchar delimiter = '|';
uchar spaces = ' ';
uchar delimiterString[2];
*destination = '\0';
strcpy(delimiterString,"|");
strcpy(localSource, theSource);
source = localSource;
if (*source) {
if (*source == delimiter) source++;
if (*(source + strlen(source) - 1) != delimiter) strcat(source, delimiterString);
sourceStart = source;
sourceEnd = source;
arrayIndex = 0;
longestElementSize = 0;
while (sourceEnd) {
sourceEnd = strchr(sourceStart, delimiter);
if (sourceEnd) {
mid(array[arrayIndex], sourceStart, 0, sourceEnd - sourceStart);
if (longestElementSize < strlen(array[arrayIndex])) longestElementSize = strlen(array[arrayIndex]);
sourceStart = ++sourceEnd;
arrayIndex++;
}
}
arraySize = arrayIndex;
for (arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) {
if (strlen(array[arrayIndex]) < longestElementSize) padAfter(array[arrayIndex], longestElementSize, spaces);
strcat(destination, array[arrayIndex]);
strcat(destination, delimiterString);
}
if (*(destination + strlen(destination) - 1) == delimiter) *(destination + strlen(destination) - 1) = '\0';
}
return(destination);
}
uchar * padAfter(uchar *source, int size, uchar theCharacter)
{
uchar *sourceStart = source;
size = size - strlen(source);
source = source + strlen(source);
while (size--) *source++ = theCharacter;
*source = '\0';
return(sourceStart);
}
/* reserved for future use */
/*
char * padBefore(char *source, int size, char theCharacter)
{
}
*/
bool choice(string clist, string fld)
{
string ts,ts1;
string tempString; /* SN 3.5 */
strconcat(ts,"|",padout(tempString, clist),"|",NULL); /* SN 3.5 */
strconcat(ts1,"|",fld,"|",NULL);
strupr(ts);
strupr(ts1);
return((bool) (strposstr(ts1,ts) != -1));
}
strptr getchoice(strptr sout, string list, byte wid, byte cnum)
{
string tmpclist;
byte i;
strcpy(tmpclist,list);
for (i=1; i < cnum; i++)
strdelete(tmpclist,0,wid+1);
strcopy(sout,tmpclist,0,wid);
return(sout);
}
void dispchoices(byte choice)
{
string tmpclist,ts;
byte i;
strcpy(tmpclist,choices_);
for (i=1; i < choice; i++)
strdelete(tmpclist,0,dispwid_+1);
for (i=0; (i < whgt_) && ((choice+i) <= numc_); i++) {
writewxy(pad(ts,strcopy(ts,tmpclist,0,dispwid_),wwid_,Right),0,1,(byte)(i+1),twinp_);
strdelete(tmpclist,0,dispwid_+1);
}
}
uchar firstchar(strptr s)
{
while(*s && (*s == ' '))
s++;
return (*s);
}
void setchoice(uchar ch)
{
string tmpclist;
byte i;
strcpy(tmpclist,choices_);
i = 1;
while ((firstchar(tmpclist) != ch) && (tmpclist[0])) {
strdelete(tmpclist,0,dispwid_+1);
i++;
}
if (firstchar(tmpclist) == ch) {
cnum_ = i;
cmd_ = Enter;
}
else {
if ((ch != Esc) || !permitesc)
audible(Pip);
}
}
strptr choose(strptr sout, string cdata, string clist, strptr fld)
{
byte tempNumber;
string tempString;
chooseExact(tempString, cdata, clist, fld);
strcpy(sout,LSC_BaseError);
fldnum--;
return(sout);
}
byte chooseExact(strptr sout, string cdata, string clist, strptr fld)
{
byte x, y, line, datawid, tlen;
string ts;
string tempcdata, tempclist;
line = 1;
cnum_ = 1;
padout(tempcdata, cdata);
padout(tempclist, clist);
strcpy(choices_,(tempclist[0] == '\0') ? tempcdata : tempclist);
if (choices_[0] == '|')
strcpy(choices_,&choices_[1]);
dispwid_ = (byte) strposch('|',choices_);
datawid = (byte) strposch('|',tempcdata);
strchcat(choices_,'|');
numc_ = (byte) (strlen(choices_)/(dispwid_+1));
whgt_ = (byte) minimum(scrhgt-2, numc_);
tlen = (byte) strlen(LSC_Choose) + 2;
wwid_ = (byte) ((dispwid_ < tlen) ? tlen : dispwid_);
x = (byte) (wherex()+datawid+1);
if ((x+wwid_+1) > scrwid) {
x = (byte) (wherex()-wwid_-1);
if (x <= 1)
x = (scrwid-wwid_)/2;
}
y = (byte) wherey();
if ((y <= 1) || ((y+whgt_+1) > scrhgt))
y = (scrhgt-whgt_)/2;
if (openwin(0,x,y,wwid_,whgt_,swc,sbc,8,"─┐│┘─└│┌",TopCnt,LSC_Choose)) {
twinp_ = curwin;
twinp_->disp = True;
dispwindow(twinp_);
dispchoices(cnum_);
do {
if (numc_ != whgt_) {
writewxy((line != cnum_) ? "\x18" : "\xC4",0,(byte)(x+3),(byte)(y+whgt_),NULL);
writewxy(((numc_-cnum_)+line>whgt_) ? "\x19" : "\xC4",0,(byte)(x+4),(byte)(y+whgt_),NULL);
}
pad(ts,getchoice(ts,choices_,dispwid_,cnum_),wwid_,Right);
writewxy(ts,_av,1,line,twinp_);
cmd_ = getkey();
if ((cmd_ >= 'a') && (cmd_ <= 'z'))
cmd_ -= 'a'-'A';
writewxy(ts,swc,1,line,twinp_);
switch (cmd_) {
case HlpKey :
if (choosehelp != NULL)
choosehelp();
break;
case UArr :
if (cnum_ > 1) {
cnum_--;
if (line > 1)
line--;
else
dispchoices(cnum_);
}
break;
case DArr :
if (cnum_ < numc_) {
cnum_++;
if (line < whgt_)
line++;
else
dispchoices((byte)(cnum_ - 2));
}
break;
case Enter :
case XeptKey :
case ' ' : break;
default : setchoice(cmd_);
}
} while ((cmd_ != Enter) && (cmd_ != XeptKey) && !(permitesc && (cmd_ == Esc)));
if (permitesc) exitcode = cmd_;
closewin(&twinp_);
if (cmd_ == Esc)
fld[0] = '\0';
else
getchoice(fld,tempcdata,datawid,cnum_);
}
sout[0] = '\0';
return((*fld == '\0') ? 0 : cnum_); /* SN 3.5 */
}
int selectfrom(byte line, string prompt, string clist, helpproc help)
{
bool tb;
int fval,
l,
p;
string ts,
picked;
winptr twinp;
string tempclist;
byte plen;
fval = 0;
plen = (byte) strlen(prompt);
twinp = NULL;
if ((plen > 0) &&
openwin(0,(scrwid-plen)/2,line,plen,1,swc,sbc,8,"─┐│┘─└│┌",TopCnt,"")) {
twinp = curwin;
twinp->disp = True;
dispwindow(twinp);
writewxy(prompt,0,1,1,twinp);
}
padout(tempclist, clist); /* SN 3.5 */
l = strposch('|',tempclist)+1;
if (l >= 0) l = strlen(tempclist);
p = (scrwid-plen)/2 + plen - l;
gotoxy(p,line + 3);
tb = permitesc;
permitesc = True;
choosehelp = help;
fval = chooseExact(ts,tempclist,tempclist,picked); /* SN 3.5 */
permitesc = tb;
choosehelp = defChooseHelp; /* SN 3.5 */
if (twinp != NULL) closewin(&twinp); /* SN 3.5 */
return(fval);
}
float txtinc(pathstr fileName, float fallBack)
{
float nextNumber;
FILE *fileStream;
int fileHandle;
long filePosition;
nextNumber = fallBack;
do {
fileHandle = sopen(fileName, O_RDWR | O_CREAT | O_BINARY, SH_DENYRW, S_IREAD | S_IWRITE);
if ((fileHandle == -1) && (errno == EACCES)) fileHandle = 0;
} while(fileHandle == 0) ;
if (fileHandle > 0) {
fileStream = fdopen(fileHandle, "r+b");
if (!feof(fileStream)) {
fgetpos(fileStream, &filePosition);
if (fread(&nextNumber, sizeof(nextNumber), 1, fileStream) == 1)
nextNumber++;
fsetpos(fileStream, &filePosition);
}
fwrite(&nextNumber, sizeof(nextNumber), 1, fileStream);
fclose(fileStream);
}
return(nextNumber);
}
/* new for 3.6 an easy to use txtinc Chris */
char* NewInc(char* fileName, unsigned long ulStart, unsigned int uPiclen)
{
unsigned long ulNextNumber;
FILE *fileStream;
int fileHandle;
long filePosition;
/* make this static so we can return it*/
static char lpszValue[40];
/* set all positions to '\0' so it is always null terminated*/
memset (lpszValue,'\0',sizeof(lpszValue));
ulNextNumber = ulStart;
do
{
fileHandle = sopen(fileName, O_RDWR | O_CREAT | O_BINARY, SH_DENYRW, S_IREAD | S_IWRITE);
if ((fileHandle == -1) && (errno == EACCES))
fileHandle = 0;
} while(fileHandle == 0) ;
if (fileHandle > 0)
{
fileStream = fdopen(fileHandle, "r+b");
if (!feof(fileStream))
{
fgetpos(fileStream, &filePosition);
if (fread(&ulNextNumber, sizeof(ulNextNumber), 1, fileStream) == 1)
ulNextNumber++;
fsetpos(fileStream, &filePosition);
}
fwrite(&ulNextNumber, sizeof(ulNextNumber), 1, fileStream);
fclose(fileStream);
}
/* convert the value back to a string */
ultoa(ulNextNumber,lpszValue,10);
/* make suer that it has th ecorrect number of digits */
lpszValue[uPiclen] = '\0';
return( lpszValue );
}
void db_work_init(void)
{
string ts;
if (!initialized) {
initialized = True;
db_crit_init();
db_curs_init();
db_funcs_init();
db_gvar_init();
db_heap_init();
db_key_init();
db_win_init();
db_mnu_init();
permitesc = False;
choosehelp = defChooseHelp;
}
}